package com.gxa.pingan.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.InsuranceManagerMapper;
import com.gxa.pingan.mapper.PointManagerMapper;
import com.gxa.pingan.mapper.UserInsKindMapper;
import com.gxa.pingan.mapper.UserMapper;
import com.gxa.pingan.pojo.InsuranceManager;
import com.gxa.pingan.pojo.PointManager;
import com.gxa.pingan.pojo.User;
import com.gxa.pingan.pojo.UserInsKind;
import com.gxa.pingan.service.InsuranceManagerService;
import com.gxa.pingan.utils.IDUtils;
import com.gxa.pingan.utils.PhoneUtils;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Transactional(rollbackFor = Exception.class)
@Service
public class InsuranceManagerServiceImpl implements InsuranceManagerService {

    @Autowired
    InsuranceManagerMapper insuranceManagerMapper;

    @Autowired
    UserInsKindMapper userInsKindMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    PointManagerMapper pointManagerMapper;


    @Override
    public List<Map> findAll() {
        return insuranceManagerMapper.findAll();
    }

    @Override
    public List<Map> findAllByPage( Integer page ,Integer size) {
        List<Map> allByPage = insuranceManagerMapper.findAllByPage((page - 1) * size, size);



        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map map : allByPage) {
            if (map.get("CLIVTAStartTime") != null){
                map.put("CLIVTAStartTime", dateFormat.format(map.get("CLIVTAStartTime")));
            }else {
                map.put("CLIVTAStartTime", map.get("CLIVTAStartTime"));
            }

            if (map.get("CLEndTime") != null){
                map.put("CLEndTime", dateFormat.format(map.get("CLEndTime")));
            }else {
                map.put("CLEndTime", map.get("CLEndTime"));
            }

            if (map.get("CLIVTAEndTime") != null){
                map.put("CLIVTAEndTime", dateFormat.format(map.get("CLIVTAEndTime")));
            }else {
                map.put("CLIVTAEndTime", map.get("CLIVTAEndTime"));
            }

            if (map.get("CLStartTime") != null){
                map.put("CLStartTime", dateFormat.format(map.get("CLStartTime")));
            }else {
                map.put("CLStartTime", map.get("CLStartTime"));
            }
        }
        return allByPage;
    }


    @Override
    public List<Map> findNameOrPhoneByPage(String nameOrPhone, Integer offset, Integer size) {
        List<Map> allByPage = insuranceManagerMapper.findNameOrPhoneByPage(nameOrPhone,(offset - 1) * size, size);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map map : allByPage) {
            if (map.get("CLIVTAStartTime") != null){
                map.put("CLIVTAStartTime", dateFormat.format(map.get("CLIVTAStartTime")));
            }else {
                map.put("CLIVTAStartTime", map.get("CLIVTAStartTime"));
            }

            if (map.get("CLEndTime") != null){
                map.put("CLEndTime", dateFormat.format(map.get("CLEndTime")));
            }else {
                map.put("CLEndTime", map.get("CLEndTime"));
            }

            if (map.get("CLIVTAEndTime") != null){
                map.put("CLIVTAEndTime", dateFormat.format(map.get("CLIVTAEndTime")));
            }else {
                map.put("CLIVTAEndTime", map.get("CLIVTAEndTime"));
            }

            if (map.get("CLStartTime") != null){
                map.put("CLStartTime", dateFormat.format(map.get("CLStartTime")));
            }else {
                map.put("CLStartTime", map.get("CLStartTime"));
            }
        }
        return allByPage;
    }

    @Override
    public void add(InsuranceManager insuranceManager) throws BusinessException {
        List<UserInsKind> list = insuranceManager.getList();
        // 去除uuid的"-",生成交强保单号
        UUID uuid = UUID.randomUUID();
        String uuidReplace = uuid.toString().replace("-", "");
        //生成商业保单号
        UUID uuid1=UUID.randomUUID();
        String uuidReplace1 = uuid.toString().replace("-", "");
        //赋值操作
        insuranceManager.setCLIVTANum(uuidReplace);
        insuranceManager.setCLNum(uuidReplace1);

        insuranceManagerMapper.add(insuranceManager);
    //对身份证和电话号码进行校验，如果对，就执行操作，如果不对就抛异常
        boolean phoneNumber = PhoneUtils.isPhoneNumber(insuranceManager.getPhone());
        boolean idNumber = IDUtils.isIDNumber(insuranceManager.getIdCard());
        if (idNumber&&phoneNumber){
            //获取添加后的主键
            Long id = insuranceManager.getId();
            //添加的时候要看这个用户在不在数据库，如果在数据库，那就不注册，如果不在就注册
            User user = userMapper.selectByIdCard(insuranceManager.getIdCard());
            if (user==null){
                int count=0;
                //添加用户
                User user1=new User();
                user1.setRelationName(insuranceManager.getName());
                user1.setPhone(insuranceManager.getPhone());
                user1.setIdCard(insuranceManager.getIdCard());

                List<User> list1 = userMapper.getList();
                for (User user2 : list1) {
                    if(!user2.getPhone().equals(insuranceManager.getPhone())){
                        count++;
                    }
                }
                if (count==list1.size()){
                    userMapper.add(user1);
                }

                else {
                    throw new BusinessException(ExceptionCode.ERROR_ID_CARD_PHONE);
                }
            }
            //给userInsKindMapper的oid赋值
            for (int i=0;i<list.size();i++){
                list.get(i).setOid(id);
            }
            userInsKindMapper.add(list);
        }
          else {
              throw new BusinessException(ExceptionCode.WRONG_IDCARD);
        }
    }

    @Override
    public void delete(Long id) {
        insuranceManagerMapper.delete(id);
        userInsKindMapper.delete(id);
    }

    @Override
    public void update(Long id, InsuranceManager insuranceManager) throws BusinessException {
      //整体思路，先删除在添加.
        delete(id);
        add(insuranceManager);
    }

    @Override
    public InsuranceManager findByIdToUpDate(Integer id) {
        InsuranceManager byIdToUpDate = insuranceManagerMapper.findByIdToUpDate(id);

        return byIdToUpDate;
    }
}
